/**
 * Created by rekey on 13-12-13.
 */
var imagemagick = require('imagemagick');
var toPromise = require('./toPromise.js');
var mimeTypes = {
  'JPEG': 'jpg',
  'PNG': 'png',
  'GIF': 'gif',
  'GIF0B': 'gif'
};

function identify(filename, callback) {
  imagemagick.identify(filename, function (err, data) {
    if (err) {
      callback({
        'error': 1,
        'message': 'file is not a image.'
      });
    } else {
      var channel_statistics = data['channel statistics'];
      callback(null, {
        size: data.filesize,
        width: data.width,
        height: data.height,
        type: mimeTypes[data.format],
        mean: [channel_statistics.red.mean.replace(/\.[\d]+ \(.*\)/, '') , channel_statistics.green.mean.replace(/\.[\d]+ \(.*\)/, '') , channel_statistics.blue.mean.replace(/\.[\d]+ \(.*\)/, '')]
      });
    }
  });
}
module.exports = function (filename) {
  return toPromise(identify)(filename);
};